// rs_anwser/pages/subject/subject.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex:1,
    collectPrompt:true,
    animation:{},
    current:0,
    subjectIndex:1,
    pageSize:10,
    wrongCount:0,
    rightCount:0,
    total:160,
    subjectList:[
     
    ],
    param:{
      subjectType:'single'
    }
  
  },
  changeSubject:function(e){
    var me = this; 
    console.log(e);
    this.setData({ current: e.detail.current, subjectIndex: e.detail.current+1});
    if(e.detail.current == this.data.subjectList.length-1){
      app.getData(this, "entry/wxapp/getSubjectList",{subjectType:'single'});
    }
  },
  updateCurrent:function(data){
    this.setData({ current: data.detail });
  },
  updateSubjectList: function (data) {
    this.setData({subjectList:data.detail});

  },
  updatePageIndex:function(data){
    this.setData({ pageIndex: data.detail });
  },

  /**
   * 选择题目选项
   */
  selectOption:function(e){
      var subject = app.subjectPage.getCurrentSubject(this);
      if(typeof(subject.isAnswered) == 'undefined'){
        //如果还没有没有答题
        var answer = subject.anwser;
        var selectOption = e.currentTarget.dataset.label;
        var options = subject.options;

        for (var i = 0; i < options.length; i++) {
          if (options[i].label == selectOption) {
            if (selectOption == answer) {
              options[i]['resultClass'] = 'result-right';
              this.setData({ rightCount: ++this.data.rightCount });
            } else {
              options[i]['resultClass'] = 'result-wrong';
              this.setData({ wrongCount: ++this.data.wrongCount });
              //选错则添加到错题本
              app.addWrong('single', subject.id);
             
            }

          } else {
            if (options[i]['label'] == answer) {
              options[i]['resultClass'] = 'result-right';
            }
          }
        }


        subject.options = options;
        subject.isAnswered = true;
        app.subjectPage.updateCurrentSubject(this,subject);
      }
     
     
  },

  /**
   * 
   */
  collect:function(e){
    app.collect('single', e.currentTarget.dataset.id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;
    app.getData(this, "entry/wxapp/getSubjectList", { subjectType: 'single' });
    var date = new Date();
    var userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    this.setData({
      refreshTime: date.toLocaleTimeString(),
      collectPrompt:userInfo.prompt.collect_prompt
    })
   
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.selectComponent('#collect_promt').startPrompt();
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },


  
})